package ig.metrica.modelos.dao;


import ig.da.abs.AbstractBasicDao;
import java.sql.SQLException;
import java.lang.Exception;
import ig.metrica.modelos.entity.ModeloorigenesdatosDTO;
import ig.metrica.modelos.entity.ModeloorigenesdatosPK;

/**
 * GenJ 2011
 * @author usuario
 *
 * Is an Class for table DB for DAO
 */
public class ModeloorigenesdatosDAO extends AbstractBasicDao{

	// -----------------------------------------------------------------
	// Constants
	// -----------------------------------------------------------------

	// -----------------------------------------------------------------
	// Attributes
	// -----------------------------------------------------------------
    private java.sql.Connection connection = null;

	// -----------------------------------------------------------------
	// Constructors
	// -----------------------------------------------------------------
    public ModeloorigenesdatosDAO ()  {

    }

	// -----------------------------------------------------------------
	// All Methods
	// -----------------------------------------------------------------

                        

    /**
     * Method for create an record of table of table
     * @return : the pk of table
     * @param dto
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public  ig.metrica.modelos.entity.ModeloorigenesdatosPK  create(ModeloorigenesdatosDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;

        try{
            String QUERY = "INSERT INTO MODELOORIGENESDATOS ( MODELO, ORIGENESDATOS ) VALUES ( ?,  ? )";

            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getModelo());
            prep.setInt (2,dto.getOrigenesdatos());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return dto.getPk();
    }


    /**
     * Update by Pk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int update (ModeloorigenesdatosDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE MODELOORIGENESDATOS SET  WHERE MODELO = ? AND ORIGENESDATOS = ?";
            prep = connection.prepareStatement(QUERY);

            prep.setInt (1,dto.getModelo());
            prep.setInt (2,dto.getOrigenesdatos());
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Delete by Pk
     * @param pk
     * @return the number record delete
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int delete (ModeloorigenesdatosPK pk ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM MODELOORIGENESDATOS WHERE MODELO = ? AND ORIGENESDATOS = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,pk .getModelo());
            prep.setInt (2,pk .getOrigenesdatos());
            
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Find by Pk
     * @param pk
     * @return DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public ModeloorigenesdatosDTO findByPk (ModeloorigenesdatosPK pk ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        ModeloorigenesdatosDTO modeloorigenesdatos = null;
        try{
            String QUERY = "SELECT MODELO, ORIGENESDATOS FROM MODELOORIGENESDATOS WHERE MODELO = ? AND ORIGENESDATOS = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,pk .getModelo());
            prep.setInt (2,pk .getOrigenesdatos());
            res = prep.executeQuery();
            if (res.next()) {
                modeloorigenesdatos = new ModeloorigenesdatosDTO(  res.getInt ( 1 ), res.getInt ( 2 ));
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,res,connection);
        }
        return modeloorigenesdatos;
    }

    /**
     * Find by All
     * @return the list DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public java.util.List<ModeloorigenesdatosDTO> findAll() throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ModeloorigenesdatosDTO> list=new java.util.ArrayList<ModeloorigenesdatosDTO>();

        try{
            String QUERY = "SELECT MODELO, ORIGENESDATOS FROM MODELOORIGENESDATOS ";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();

            while (res.next()) {
                list.add(new ModeloorigenesdatosDTO(  res.getInt ( 1 ), res.getInt ( 2 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }



    /**
     * The List By modelo
     * @param modelo
     * @return list of ModeloorigenesdatosDTO
     */
    public java.util.List<ModeloorigenesdatosDTO> findByModelo (Integer  modelo) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ModeloorigenesdatosDTO> list=new java.util.ArrayList<ModeloorigenesdatosDTO>();

        try{
            String QUERY = "SELECT MODELO, ORIGENESDATOS FROM MODELOORIGENESDATOS WHERE MODELO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,modelo);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ModeloorigenesdatosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By origenesdatos
     * @param origenesdatos
     * @return list of ModeloorigenesdatosDTO
     */
    public java.util.List<ModeloorigenesdatosDTO> findByOrigenesdatos (Integer  origenesdatos) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ModeloorigenesdatosDTO> list=new java.util.ArrayList<ModeloorigenesdatosDTO>();

        try{
            String QUERY = "SELECT MODELO, ORIGENESDATOS FROM MODELOORIGENESDATOS WHERE ORIGENESDATOS = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,origenesdatos);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ModeloorigenesdatosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }





    /**
     * Open the connection
     * @return
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private java.sql.Connection open() throws ClassNotFoundException, java.sql.SQLException,Exception {
    	return getConnection("METRICADW");
    }



}
